
package com.min.main.config;

import cn.hutool.core.collection.CollUtil;
import com.min.common.entity.MinReource;
import com.min.main.config.perms.MinFilter;
import com.min.main.config.perms.MinRealm;
import com.min.main.service.RoleService;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Shiro配置
 *
 * @author fengt
 */
@Configuration
public class ShiroConfig {

    @Autowired
    RoleService roleService;

    @Bean("securityManager")
    public SecurityManager securityManager(MinRealm minRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(minRealm);
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        Map<String, Filter> filters = new HashMap<>();
        filters.put("auth", new MinFilter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();

        // 不需要授权访问的路径
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/user/login", "anon");
        filterMap.put("/monitor/**", "anon");
        filterMap.put("/user/logout", "anon");
        filterMap.put("/user/captcha.jpg", "anon");

        // filterMap.put("/account/getVersion", "oauth2,perms[/resource,111]");

        /**
         * 从数据库中动态加入权限，免去方法上写注解的形式授权
         * 角色具备一个就可以 roles["role1","role2"]
         * 角色同时具备 roles["role1,role2"]
         * 权限 perms["perms1", ""perms2]
         */
        if (this.roleService != null) {
            List<MinReource> res = this.roleService.getMinResoures();
            if (CollUtil.isNotEmpty(res)) {
                res.forEach(r -> {
                    filterMap.putIfAbsent(r.getName(), "auth,perms[" + r.getPerms() + "]");
                });
            }
        }

        filterMap.put("/**", "auth");


        shiroFilter.setFilterChainDefinitionMap(filterMap);


        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
